<template>
  <md-nav-bar
      title="" hideLeft
      :bgInfo="{
        url: 'https://cdn.xmxiaomai.com/isong_static/evaluation/indexTopBg.png',
        showLogo:false,
        type: 'bg'
      }"
  >
    <view class="rule-wrap">
      <view>测评须知：</view>
      <view>一、参评作品按以下选项提交，信息越多对作品评价越精准：</view>
      <view>1、音频：MP3、WAV 格式文件。</view>
      <view>2、歌词：文字格式。</view>
      <view>二、用户提前将测评材料，上传到微信的“文件传输助手”上，可直接调用。</view>
    </view>
  </md-nav-bar>
  <!-- 顶部背景与标题 -->
  <view class="header-bg">
    <view class="header-content">
      <text class="title">我的音乐</text>
      <text class="guide" @click="showGuide">操作指引</text>
    </view>
  </view>
  <view class="container">
    <!-- 音乐卡片列表 -->
    <view class="music-list">
      <md-page-list :api="api.Music.myList" @change="pageChange">
        <view class="music-card" v-for="(item, idx) in data.list" :key="idx" @click="onToMyMusicDetail(item)">
          <image
            class="music-img"
            :src="item?.coverUrl || 'https://cdn.xmxiaomai.com/isong_static/music/music-default-avatar.png'"
            mode="aspectFill"></image>
          <view class="music-info">
            <text class="music-title">{{ item.name }}</text>
            <text class="music-time">{{ item.createTime }}</text>
            <view class="music-meta">
              <view class="meta-item">
                <image class="meta-icon" src="https://cdn.xmxiaomai.com/isong_static/square/%E8%B5%9E.png"></image>
                <text>{{ item.likesNum }}</text>
              </view>
              <view class="meta-item">
                <image
                  class="meta-icon"
                  src="https://cdn.xmxiaomai.com/isong_static/square/%E8%AF%84%E8%AE%BA.png"></image>
                <text>{{ item.commentNum }}</text>
              </view>
              <view class="meta-item">
                <image class="meta-icon" src="https://cdn.xmxiaomai.com/isong_static/square/%E8%BD%AC.png"></image>
                <text>{{ item.collectNum }}</text>
              </view>
            </view>
          </view>
          <view class="music-score">
            <template v-if="item.evaluationStatus == 0">
              <text class="score">
                {{ item.aiScore }}
              </text>
              <text class="score-unit">分</text>
            </template>
            <tampalte v-else>
              <view v-if="item.evaluationStatus == 1">测评中</view>
              <view v-if="item.evaluationStatus == 2" style="color: red">测评失败</view>
            </tampalte>
          </view>
        </view>
      </md-page-list>
    </view>

    <!-- 添加音乐按钮 -->
    <view class="add-btn-wrap">
      <button class="add-btn" @click="addMusic">上传作品</button>
    </view>
    <bottom-tab-bar :current="2" />
  </view>
</template>

<script lang="ts" setup>
import { reactive } from 'vue';
import api from '@/api/index';
import type { Music } from '@/api/data';
import {onLoad, onShow} from "@dcloudio/uni-app";
import { checkLoginAndNavigate } from '@/utils/util';

const data = reactive({
  list: [] as Music.List.Data['list'],
});

// 添加音乐
const addMusic = () => {
  uni.navigateTo({
    url: `/pages/sub/my/addMusic`,
  });
};
// 打开我的音乐详情
const onToMyMusicDetail = (item: Music.List.ListVo) => {
  uni.navigateTo({
    url: `/pages/sub/square/myMusicDetail?id=` + item.id,
  });
};

// 数据回调
const pageChange = (list: Music.List.Data['list']) => {
  data.list = list;
};

const showGuide = () => {
  uni.navigateTo({
    url: '/pages/sub/my/evaluateGuide'
  });
};

onShow(()=>{
  // 使用统一的登录检查函数，避免重复跳转登录页
  if (!checkLoginAndNavigate()) {
    return;
  }
  setTimeout(()=>{
    const hasVisited = uni.getStorageSync('hasVisitedAddMusic');
    if (!hasVisited) {
      uni.setStorageSync('hasVisitedAddMusic', true);
      uni.navigateTo({
        url: '/pages/sub/my/evaluateGuide'
      });
    }
  },200)
})
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  // background: #f6fcfa;
  padding-bottom: 120rpx;
}
.header-bg {
  width: 100vw;
  height: 220rpx;
  margin-top: -150rpx;
  position: relative;
}
.header-content {
  position: absolute;
  top: 80rpx;
  left: 40rpx;
  right: 40rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.title {
  color: #fff;
  font-size: 36rpx;
  font-weight: bold;
}

.guide {
  color: #fff;
}

.header-icon {
  width: 60rpx;
  height: 60rpx;
}
.music-list {
  margin-top: -60rpx;
  padding: 0 24rpx;
}
.music-card {
  display: flex;
  align-items: center;
  background: linear-gradient(180deg, #d9f2ec 0%, #e7f3f2 100%);
  box-shadow: 0rpx 8rpx 11rpx 6rpx rgba(43, 42, 42, 0.09), inset 0rpx 0rpx 8rpx 4rpx #ffffff;
  border-radius: 50rpx;
  border: 3rpx solid #deb893;
  margin-bottom: 32rpx;
  padding: 24rpx 32rpx;
  position: relative;
}
.music-img {
  width: 120rpx;
  height: 120rpx;
  border-radius: 20rpx;
  margin-right: 24rpx;
  flex-shrink: 0;
}
.music-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.music-title {
  font-size: 32rpx;
  color: #222;
  font-weight: 600;
  margin-bottom: 8rpx;
  width: 320rpx;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.music-time {
  font-size: 24rpx;
  color: #888;
  margin-bottom: 12rpx;
}
.music-meta {
  display: flex;
  align-items: center;
}
.meta-item {
  display: flex;
  align-items: center;
  margin-right: 32rpx;
}
.meta-icon {
  width: 28rpx;
  height: 28rpx;
  margin-right: 6rpx;
}
.music-score {
  display: flex;
  align-items: baseline;
  margin-left: 24rpx;
}
.score {
  font-size: 56rpx;
  color: #1fa37a;
  font-weight: bold;
  line-height: 1;
}
.score-unit {
  font-size: 24rpx;
  color: #717170;
  margin-top: -8rpx;
}
.add-btn-wrap {
  position: fixed;
  bottom: calc(48px + env(safe-area-inset-bottom) + 75rpx);
  left: 40rpx;
  margin: 60rpx 0 0 0;
  display: flex;
  justify-content: center;
}
.add-btn {
  width: 670rpx;
  height: 90rpx;
  background: #1fa37a;
  color: #fff;
  font-size: 36rpx;
  border-radius: 60rpx;
  font-weight: 600;
  box-shadow: 0 4rpx 16rpx 0 #e0e0e0;
  border: none;
}
.tabbar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100vw;
  height: 110rpx;
  background: #184c3a;
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 99;
  box-shadow: 0 -2rpx 12rpx 0 #e0e0e0;
}
.tabbar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
}
.tabbar-icon {
  width: 48rpx;
  height: 48rpx;
  margin-bottom: 4rpx;
  opacity: 0.7;
}
.tabbar-icon.active {
  width: 80rpx;
  height: 80rpx;
  margin-bottom: 0;
  margin-top: -32rpx;
  background: #f6fcfa;
  border-radius: 50%;
  box-shadow: 0 4rpx 16rpx 0 #e0e0e0;
  opacity: 1;
}
.tabbar-text {
  font-size: 22rpx;
  color: #f6fcfa;
  opacity: 0.7;
}
.tabbar-text.active {
  color: #1fa37a;
  font-size: 28rpx;
  font-weight: bold;
  opacity: 1;
}
.rule-wrap {
  color: white;
  padding: 0 40rpx 0;
  font-size: 24rpx;
  margin-top: -30rpx;
  z-index: 2;
  position: relative;
}
</style>
